package cn.yunhe.servlet;

import cn.yunhe.biz.AdminService;
import cn.yunhe.pojo.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/AdminRegServlet")
public class AdminRegServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name= request.getParameter("username");
        String pwd= request.getParameter("password");

        Admin admin = new Admin(name,pwd);
        AdminService adminService=new AdminService();
        int flag=adminService.addAdmin(admin);

        if (flag>0){
            response.sendRedirect("login.html");
        }else {
            response.setContentType("type/text,utf-8");
            PrintWriter out = response.getWriter();
            out.print("<script>alert('注册失败 请重试...')</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }
}
